// subpackagesA/pages/signin/signin.ts
import { routingInspection } from '../../../api/clockin'
import { createStoreBindings } from "mobx-miniprogram-bindings";
import { store } from "../../../store/index"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    inputOption: {
      maxHeight: 500,
      minHeight: 100
    },
    isLoading: false,
    fileList: [],
    powerSupply: '',
    temperature: '',
    pressure: '',
    traffic: '',
    filling: '',
    cabinet: '',
    record: '',
    id: -1,
    comID: -1,
    show: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.storeBindings = createStoreBindings(this, {
      store,
      fields: ["signinName", "HrzId", "HrzType"],
      actions: ["updateName", "updateId", "updateType"],
    });

  },
  onClickLeft() {
    wx.navigateBack({
      delta: 1
    })
  },
  onClose() {
    this.setData({
      show: false,
    })
    wx.navigateBack({
      delta: 1
    })
  },
  afterRead(event) {
    const { file } = event.detail;
    const _this = this
    const { fileList = [] } = _this.data;
    fileList.push({ ...file, url: file.url });
    _this.setData({ fileList });
    // console.log(file.url)
  },
  deleteImg(e) {
    const index = e.detail.index
    this.data.fileList.splice([index], 1)
    this.setData({
      fileList: this.data.fileList
    });
    let img_list = [];
    this.data.fileList.forEach((e) => {
      img_list.push(e.url)
    });
  },
  async onSubmit() {
    this.setData({
      isLoading: true
    })
    const imgUrlList = this.data.fileList
    const { powerSupply,
      temperature,
      pressure,
      traffic,
      filling,
      cabinet,
      record,
      id,
      comID
    } = this.data
    let list = ''
    for (let i = 0; imgUrlList.length > i; i++) {
      const res = await this.upload(imgUrlList[i].url)
      list += JSON.parse(res.data).msg + ';'
    }
    let data = {
      siteType: store.HrzType,
      siteId: store.HrzId,
      maintainerId: id,
      comID: comID,
      powerSupply: powerSupply,
      temperature: temperature,
      pressure: pressure,
      traffic: traffic,
      filling: filling,
      cabinet: cabinet,
      record: record,
      picture: list
    }
    console.log(list)
    await console.log(data)
    const res = this.hasNoEmptyValues(data)
    if (store.signinName) {
      if (res) {
        routingInspection(data).then((res) => {
          if (res.data.code == 200) {
            wx.showToast({
              title: '提交成功'
            })
            this.setData({
              isLoading: false,
              powerSupply: '',
              temperature: '',
              pressure: '',
              traffic: '',
              filling: '',
              cabinet: '',
              record: '',
              fileList: []
            })
          } else {
            wx.showToast({
              title: '提交失败',
              icon: 'error'
            })
            this.setData({
              isLoading: false
            })
          }
        }).catch(err => {
          this.setData({
            isLoading: false
          })
        })
      } else {
        this.setData({
          isLoading: false
        })
        wx.showToast({
          title: '缺少必填项',
          icon: 'error'
        })
      }
    } else {
      wx.showToast({
        title: '请打卡签到',
        icon: 'error'
      })
    }

  },
  hasNoEmptyValues(obj) {
    for (let key in obj) {
      if (obj[key] === null || obj[key] === '' || obj[key] === undefined) {
        return false; // 如果找到空值，返回false
      }
    }
    return true; // 如果没有找到空值，返回true
  },
  upload(file) {
    return new Promise((resolve, reject) => {
      wx.uploadFile({
        // url: 'http://192.168.2.48:8080/MobileLinkCenter/picture/pictureUpload', // 仅为示例，非真实的接口地址
        url: 'https://www.hbdinkon.cn:10020/MobileLinkCenter/picture/pictureUpload',
        filePath: file,
        name: 'file',
        success(res) {
          resolve(res)
        },
        fail(error) {
          reject(error);
        },
      });
    })
  },
  history() {
    wx.navigateTo({
      url: '/subpackagesA/pages/patrolhistory/patrolhistory'
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (!store.signinName) {
      this.setData({
        show: true,
      })
    }

    this.setData({
      id: wx.getStorageSync("userInfo").data.userID,
      comID: wx.getStorageSync("userInfo").data.comID
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    this.storeBindings.destroyStoreBindings();
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})